

package com.bei_en.teacher.admin.service.questions;

import java.util.ArrayList;
import java.util.List;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.bei_en.teacher.admin.service.BaseService;
import com.bei_en.teacher.admin.model.questions.bean.QuestionsCollection;
import com.bei_en.teacher.admin.model.questions.vo.QuestionsCollectionVo;
import com.bei_en.teacher.admin.model.questions.condition.QuestionsCollectionCondition;
import com.bei_en.teacher.admin.persistence.dao.questions.QuestionsCollectionDao;

/**
 * 
 * @author Guocg
 * @date 2016-8-21 21:28:03
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class QuestionsCollectionService extends BaseService{

    @Autowired
    private QuestionsCollectionDao questionsCollectionDao;

    public void insert(QuestionsCollectionVo model) {

    	 questionsCollectionDao.insert(model.getQuestionsCollection());
    }

//    /**
//     * 插入并返回生成的id
//     * @param model
//     * @return
//     */
//    public Integer merge(QuestionsCollectionVo model) {
//
//	questionsCollectionDao.insert(model.getQuestionsCollection());
//	return questionsCollectionDao.findLastInsertedId();
//
//    }

    public void delete(Integer modelInteger) {

    	questionsCollectionDao.delete(modelInteger);
    }

    public QuestionsCollectionVo findById(Integer modelInteger) {

    	QuestionsCollectionVo model = new QuestionsCollectionVo(questionsCollectionDao.findById(modelInteger));
    	return model;
    }

    public void update(QuestionsCollectionVo model) {

    	questionsCollectionDao.update(model.getQuestionsCollection());
    }

    /**
     * 动态更新,只更新非null的值
     * @param model
     */
    public void updateSelective(QuestionsCollectionVo model) {

    	questionsCollectionDao.updateSelective(model.getQuestionsCollection());
    }

    public int countAll() {

    	return questionsCollectionDao.countAll();
    }

    public List<QuestionsCollectionVo> findAll() {

		List<QuestionsCollectionVo> list = new ArrayList<QuestionsCollectionVo>();
		List<QuestionsCollection> list1 = questionsCollectionDao.findAll();
		for (QuestionsCollection model : list1) {
		    list.add(new QuestionsCollectionVo(model));
		}
		return list;
    }

    public List<Integer> findAllIds() {

    	return questionsCollectionDao.findAllIds();
    }

    /**
     * 分页查询，包括总数和内容
     * @param Vo  返回总数
     * @return  返回内容
     */
    public List<QuestionsCollectionVo> findQuestionsCollectionPage(QuestionsCollectionCondition vo) {

		Integer totalCount = searchQuestionsCollectionNum(vo);
		List<QuestionsCollectionVo> list = new ArrayList<QuestionsCollectionVo>();
		vo.setTotalCount(totalCount);
	
		if (totalCount>0) {
		    vo.setOrderField(vo.getOrderField()!=null ? vo.getOrderField() : "id");
		    vo.setOrderDirection(vo.getOrderDirection()!=null ? vo.getOrderDirection() : "desc");
	
		    RowBounds rb = new RowBounds(vo.getStartIndex(), vo.getPageSize());
		    List<QuestionsCollection> list1 = questionsCollectionDao.findPageBreakByCondition(vo, rb);
		    for (QuestionsCollection model : list1) {
		    	list.add(new QuestionsCollectionVo(model));
		    }
	
		}

		return list;
    }

    public Integer searchQuestionsCollectionNum(QuestionsCollectionCondition Vo) {

    	return questionsCollectionDao.findNumberByCondition(Vo);
    }

}
